import {
	newSearch,
	getHotSearch
} from '@/api/wtk.js'
import {
	httpTokenRequest
} from '../../utils/http.js';
import { hotSearch, getHistorySearch, newConsultSearch } from '../../api/consult.js'
export default {
	components: {

	},
	data() {
		return {
			noAction: true, //进入本界面后是否操作过
			inputValue: "",
			hotList: [], //热门搜索
			historyList: [], //历史搜索
			pageSize: 10, //每页数据量
			pageNo: 1, //页码
			hotIcon: require('../../static/img/wenTiKu/hotIcon.png'),
			page: 1,
			isInputFocus: true,
			hotSearchStr: '',
			type: 1,
		}
	},


	onShow() {
	},
	onLoad(e) {
		// type来源 1 问题库 2资讯库
		let { type } = e
		this.type = type
		if (type == 1) {
			this.loadHotSearch()
			this.loadHistorySearch()
			this.getHotSearch()
		} else if (type == 2) {
			this.getHotSearchStr()
			this.getHistorySearch()
		}
	},
	onReady() { },
	onReachBottom() { },
	methods: {
		async getDataList(value) {
			if (value == '' || value == undefined || value == null) {
				value = this.hotSearchStr
			}
			let params = {
				keyword: value,
				type: 'issue',
				page: this.page,
				limit: 15
			};
			let res = await newSearch(params);
			console.log(res);
		},
		//获取输入框热门搜索
		async getHotSearch() {
			getHotSearch({
				type: 1
			}).then(res => {
				if (res.code == 1) {
					this.hotSearchStr = res.data[0].content
				}
			})
		},
		//取消
		cancel() {
			this.noAction = true
			this.loadHotSearch()
			this.loadHistorySearch()
			this.inputValue = ""
		},

		searchQuestion(text) {
			uni.showLoading({
				title: '加载中...',
				mask: true
			})
			let userInfo = uni.getStorageSync('userInfo')

			if (text == '' || text == undefined || text == null) {
				uni.navigateTo({
					url: '/pages/webView/webView?keyword=' + this.hotSearchStr + '&deviceid=' +
						userInfo.id
				})
			} else {
				uni.navigateTo({
					url: '/pages/webView/webView?keyword=' + text + '&deviceid=' +
						userInfo.id
				})

			}
			uni.hideLoading();
			this.getDataList(text)

		},

		//获取热门搜索
		loadHotSearch() {
			httpTokenRequest({
				method: 'POST',
				url: this.$paths.questHotSearch,
				params: {}
			})
				.then(res => {
					if (res.code == 1) {
						this.hotList = res.data;
					}
				})
				.catch(err => {
					console.log('错误回调', err);
				});
		},
		//获取历史搜索记录
		loadHistorySearch() {
			httpTokenRequest({
				method: 'POST',
				url: this.$paths.getHistorySearch,
				params: {}
			})
				.then(res => {
					if (res.code == 1) {
						this.historyList = res.data
					}
				})
				.catch(err => {
					console.log('错误回调', err);
				});
		},
		//删除历史记录
		deleteHistorySearch() {
			httpTokenRequest({
				method: 'POST',
				url: this.$paths.delHistorySearch,
				params: {}
			})
				.then(res => {
					if (res.code == 1) {
						this.loadHistorySearch()
					}
				})
				.catch(err => {
					console.log('错误回调', err);
				});
		},

		//获取资讯输入框热门搜索
		getHotSearchStr() {
			hotSearch({
				type: 7
			}).then(res => {
				if (res.code == 1) {
					this.hotSearchStr = res.data[0].content
					this.hotList = res.data;
				}
			})
		},

		//获取资讯历史搜索记录
		getHistorySearch(type = '') {
			getHistorySearch().then(res => {
				if (res.code == '1') {
					let arr = []
					res.data.forEach(val => {
						let obj = {
							name: val
						}
						arr.push(obj)
					})
					this.historyList = arr
				} else {
					this.$used.msg(res.msg);
				}
			})
		},

		//清除资讯历史搜索
		delHistorySearch() {
			delHistorySearch().then(res => {
				this.$used.msg(res.msg);
				if (res.code == '1') {
					this.historyList = []
				}
			})
		},

		//删除
		deleteHistory() {
			if (this.type == 1) {
				this.deleteHistorySearch()
			} else if (this.type == 2) {
				this.delHistorySearch()
			}
		},

		//资讯搜索
		searchConsult(text) {
			uni.showLoading({
				title: '加载中...',
				mask: true
			})
			let userInfo = uni.getStorageSync('userInfo')
			if (text == '' || text == undefined || text == null) {
				uni.navigateTo({
					url: '/pages/webView/webView?type=7&keyword=' + this.hotSearchStr + '&deviceid=' +
						userInfo.id
				})
			} else {
				uni.navigateTo({
					url: '/pages/webView/webView?type=7&keyword=' + text + '&deviceid=' + userInfo.id
				})
			}
			uni.hideLoading();
			this.getDataList1(text)
		},

		//获取数据
		async getDataList1(value) {
			if (value == '' || value == undefined || value == null) {
				value = this.hotSearchStr
			}
			let params = {
				keyword: value,
				type: 'information',
				page: this.page,
				limit: 10
			};
			let res = await newConsultSearch(params);

		},

		//搜索
		search(txt) {
			if (this.type == 1) {
				this.searchQuestion(txt)
			} else if (this.type == 2) {
				this.searchConsult(txt)
			}
		},
	}
}